/*日志落地类的实现*/

#ifndef _M_SINK__
#define _M_SINK__

#include <fstream>
#include <cassert>
#include <memory>
#include <sstream>
#include "util.hpp"

namespace Alpaca
{
    class LogSink
    {
    public:
        using ptr = std::shared_ptr<LogSink>;
        LogSink() {}
        virtual ~LogSink() {}
        virtual void log(const char *data, size_t len) = 0;
    };

    class StdoutSink : public LogSink
    {
    public:
        void log(const char *data, size_t len)
        {
            std::cout.write(data, len);
        }
    };

    class FileSink : public LogSink
    {
    public:
        // 构造时需要文件名打开文件,并保留句柄
        FileSink(const std::string &pathname) : _pathname(pathname)
        {
            // 创建文件所在路径
            util::File::create_directory(util::File::path(pathname));
            // 打开文件
            _ofs.open(pathname, std::ios::binary | std::ios::app);
            assert(_ofs.is_open());
        }

        // 将日志信息写入文件
        void log(const char *data, size_t len)
        {
            _ofs.write(data, len);
            assert(_ofs.good());
        }

    private:
        std::string _pathname;
        std::ofstream _ofs;
    };

    class RollBySizeSink : public LogSink
    {
    public:
        RollBySizeSink(const std::string &basename, size_t max_size)
            : _basename(basename), _max_size(max_size), _cur_size(0), _name_count(1)
        {
            std::string pathname = createNewFile();

            // 创建文件所在路径
            util::File::create_directory(util::File::path(pathname));
            // 打开文件
            _ofs.open(pathname, std::ios::binary | std::ios::app);
            assert(_ofs.is_open());
        }

        void log(const char *data, size_t len)
        {
            // 文件过大时创建一个新文件
            if (_cur_size > _max_size)
            {
                std::string pathname = createNewFile();
                _ofs.close();
                _ofs.open(pathname, std::ios::binary | std::ios::app);
                assert(_ofs.is_open());
                _cur_size = 0;
            }
            _ofs.write(data, len);
            assert(_ofs.good());
            _cur_size += len;
        }

        // 根据时间获取文件的拓展名
        std::string createNewFile()
        {
            time_t t = util::Date::now();
            struct tm lt;
            localtime_r(&t, &lt);
            std::stringstream filename;
            filename << _basename;
            filename << lt.tm_year + 1900 << lt.tm_mon + 1
                     << lt.tm_mday << lt.tm_hour
                     << lt.tm_min << lt.tm_sec;
            filename << "-";
            filename << _name_count++;
            filename << ".log";
            return filename.str();
        }

    private:
        size_t _name_count;
        std::string _basename;
        std::ofstream _ofs;
        size_t _max_size;
        size_t _cur_size;
    };

    class SinkFactory
    {
    public:
        template <class Sinktype, class... Args>
        static LogSink::ptr create(Args &&...args)
        {
            return std::make_shared<Sinktype>(std::forward<Args>(args)...);
        }
    };
}

#endif